What is Python? And Why Should You Care?

Because it's named after “Monty Python’s Flying Circus”

Python is a high-level interpreted programming language.

high level = there are many things that come with python that you (as a developer) do not have to code from scratch

interpreted = you are a scientist, not an engineer, you want to be able to interact with your data, your program, or your script without having to compile it before being presented results, gone are the days of pode/compile/pray/test/code/re-compile, you can write 'throw away' scrips and move on.

it's free

it's extensiable

Comments

You use comments to document what you are doing in your code

you never know, you might have to look at your code 3 months from now, and will need to figure out what you did withought going line by line and re-figuring out your logic


In [ ]:
# this is a python comment, it spans 1 line, and 1 line only

'''
this is not a multi-line comment
you use 3 single quotes
this is also called a docstring.  
this is what is printed out when you call help
'''

"""
you can also use
3 double quotes
"""

You can do math in python!


In [ ]:
1 + 1 #2

9 - 1 # 8

2 * 3 #6

2**10 #1024

8/4 #2

9/2 #4

9/2.0 #4.5

9 + 2 * 3

(9 + 2) * 3

In [3]:



Out[3]:
1024

Variables

Think of variables like your introduction to algebra back in elementary school, when you wrote something like:

let $x = 3$

you can do the same in python. the variable on the left side of the equal sign will be assigned the value on the right side.


In [5]:
pizza = 3

to see the value of a variable we can either just put the variable on a separate line at the end (ipython specific function) or tell python to print the variable


In [9]:
x


Out[9]:
3

In [12]:
print x # python 2.7


3

In [13]:
print(x) #python 2.7 or 3+


3

Built-in types

Variables can be anything you can think about:

  • Booleans are either True or False.
  • Numbers can be integers (1 and 2), floats (1.1 and 1.2), fractions (1/2 and 2/3), or even complex numbers.
  • Strings are sequences of Unicode characters, e.g. an html document.
  • Bytes and byte arrays, e.g. a jpeg image file.
  • Lists are ordered sequences of values.
  • Tuples are ordered, immutable sequences of values.
  • Sets are unordered bags of values.
  • Dictionaries are unordered bags of key-value pairs.

more built-in types:

http://docs.python.org/2/library/stdtypes.html

http://www.diveintopython3.net/native-datatypes.html


In [14]:
x = True
y = False
z = 3
a = 2.7
b = 'welcome to the csaph python intro'
print x, y, z, a, b


True False 3 2.7 welcome to the csaph python intro

Python is a dynamically typed language

variables can be re-used and re-assigned on-the-fly


In [ ]:
x = True
print x

# reverse inequalities by using 'not'
print not x

x = False
print x

x = 3
print x

x = 3.14
print x

x = "hello!"
print x

Comparisons and inequalities

Sometimes when you are working with a dataset (or any piece of data) you will want to make comparisions.

For example, you would like to know if: $$ \text{age} > 25 $$

We can do this and other forms of comparisons and inequalities in python as well (think back to your early math days!)


In [15]:
30 > 25 #true

25 > 30 #false

# assign some values to some variables
age1 = 25
age2 = 30

age1 > age2 # false

age1 <= age2 # true

age1 = age2 # false

age1 != age2 # true

3.0 == 3 # true

"hello" == "Hello" #false


Out[15]:
False

Strings

Eventually you will have to deal with strings.

an example is when you have an ID column like 2014-255-001 where the ID is represented as YEAR-CENSUS_TRACT_ID

So it's good to have some general idea of strings now, we will cover more in later sections


In [ ]:
# this is a string
"Hello"

# this is also a string
'Hello'

# it does not matter if you use " or ', just be consistent

In [ ]:
# concatenation
"Hello" + "everyone" + "attending"

"hello " + "everyone"

"hello" + " everyone"

In [ ]:

Exercsise

Using only

  • the numbers 4 and 6
  • the math operations: +, -, *, /
  • comparison symbol <
  • no string longer than 10 characters

In [16]:
# print the number 8

(6 - 4) * 4


Out[16]:
8

In [19]:
# print the number 102

( 4+ 6) * (4 + 6) + (6 - 4)

str(6+4) + str(2)


Out[19]:
'102'

In [20]:
# print the string "Tis but a flesh wound"

'This but a ' + 'flesh wound'


Out[20]:
'This but a flesh wound'

In [21]:
# print the boolean value True
6 > 4


Out[21]:
True